package com.tanhua.manage.service;


import cn.hutool.core.convert.Convert;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.tanhua.dubbo.server.api.ManageMomentsApi;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.manage.mapper.UserInfoMapper;
import com.tanhua.manage.pojo.UserInfo;
import com.tanhua.manage.vo.CommentsVo;
import com.tanhua.manage.vo.MessageVo;
import com.tanhua.manage.vo.PageResult;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {


    @Reference(version = "1.0.0")
    private ManageMomentsApi manageMomentsApi;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
    private UserInfoMapper userInfoMapper;


    public PageResult queryMessageListById(Integer page, Integer pageSize, Long uid, String sortOrder, String sortProp) {
        //定义PageResult对象,最终返回前台
        PageResult pageResult = new PageResult();
        //设置分页参数
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        //获取dubbo的mongodb查询,返回一个pageinfo对象,
        PageInfo<Publish> pageInfo = null;
        pageInfo = manageMomentsApi.queryMomentsListById(uid, page, pageSize, sortProp, sortOrder);
        //获取查询的列表数据
        List<Publish> records = pageInfo.getRecords();
        //定义返回前台的items集合,将records中的数据放入items
        List<MessageVo> messageVoList = new ArrayList<>();
        for (Publish record : records) {
            MessageVo messageVo = new MessageVo();
            messageVo.setId(record.getId().toHexString());
            messageVo.setUserId(record.getUserId());
            messageVo.setText(record.getText());
            messageVo.setCreateDate(record.getCreated());
            //随机生成
            messageVo.setReportCount(RandomUtils.nextInt(50, 80));
            messageVo.setLikeCount(Convert.toInt(quanZiApi.queryLikeCount(record.getId().toString())));//点赞数量
            messageVo.setCommentCount(Convert.toInt(quanZiApi.queryCommentCount(record.getId().toString())));
            messageVo.setForwardingCount(RandomUtils.nextInt(0, 50));

            messageVoList.add(messageVo);
        }
        //装入总数
        pageResult.setCounts(pageInfo.getTotal());

        pageResult.setItems(messageVoList);


        return pageResult;
    }


    /**
     * 根据动态ID查询评论
     *
     * @param messageID
     * @param pageSize
     * @param page
     * @param sortProp
     * @param sortOrder
     * @return
     */
    public PageResult queryMessagesByMessageId(Integer page, Integer pageSize, String messageID, String sortProp, String sortOrder) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        PageInfo<Comment> pageInfo = null;
        pageInfo = this.manageMomentsApi.queryCommentsListById(messageID, page, pageSize, sortProp, sortOrder);
        List<Comment> records = pageInfo.getRecords();
        List<CommentsVo> CommentsVoList = new ArrayList<>();
        for (Comment record : records) {

            CommentsVo CommentsVo = new CommentsVo();
            CommentsVo.setId(record.getId().toHexString());
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", record.getUserId());
            UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
            CommentsVo.setNickname(userInfo.getNickName());
            CommentsVo.setContent(record.getContent());
            CommentsVo.setUserId(record.getUserId());
            CommentsVo.setCreateDate(record.getCreated().toString());

            CommentsVoList.add(CommentsVo);
        }
        pageResult.setItems(CommentsVoList);
        return pageResult;
    }

}

